package com.company.app.dao;

import java.text.MessageFormat;

import org.mybatis.spring.support.SqlSessionDaoSupport;

/**
 * The Class AbstractBaseDAO.
 *
 * @param <T> the generic type
 */
public abstract class AbstractBaseDAO<T> extends SqlSessionDaoSupport implements BaseInterfaceDAO<T> {

	/** The Constant MAPPER_NAMESPACE. */
	private static final String MAPPER_NAMESPACE = "com.company.app.dao.";

	/** The Constant MAPPER_SUFIX. */
	private static final String MAPPER_SUFIX = "Mapper";
	
	/** The Constant INSERT. */
	private static final String INSERT = "insert";
	
	/** The Constant UPDATE. */
	private static final String UPDATE = "update";
	
	/** The Constant DELETE. */
	private static final String DELETE = "delete";
	
	
	/**	
	 * Gets the id statement formarted.
	 *
	 * @param className the class name
	 * @param type the type
	 * @return the id statement formarted
	 */
	private String getIdStatementFormarted (String className, String type){
		return MessageFormat.format("{0}{1}{2}.{3}{4}", MAPPER_NAMESPACE , className ,MAPPER_SUFIX, type , className);
	}
	
	/* (non-Javadoc)
	 * @see com.company.app.dao.BaseInterfaceDAO#createEntity(java.lang.Object)
	 */
	@Override
	public T createEntity(T entity){
		this.getSqlSession().insert(getIdStatementFormarted(entity.getClass().getSimpleName(), INSERT),entity);
		return entity;
	}

	/* (non-Javadoc)
	 * @see com.company.app.dao.BaseInterfaceDAO#updateEntity(java.lang.Object)
	 */
	@Override
	public T updateEntity(T entity) {
		this.getSqlSession().update(getIdStatementFormarted(entity.getClass().getSimpleName(), UPDATE), entity);
		return entity;
	}

	/* (non-Javadoc)
	 * @see com.company.app.dao.BaseInterfaceDAO#deleteEntity(java.lang.Object)
	 */
	@Override
	public T deleteEntity(T entity) {
		this.getSqlSession().delete(getIdStatementFormarted(entity.getClass().getSimpleName(), DELETE), entity);
		return entity;
	}
	
}
